﻿/*****************************************************************************  

 Version: 0.1
 Website: http://www.visuagile.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VisualAgile.Model;
using System.Collections;

namespace VisualAgile.DTO.Mappers
{
    public class StoryDTOMapper:BaseDTOMapper
    {
        public static StoryDTO MapToDTO(Story model)
        {
            StoryDTO dto = new StoryDTO();
            dto.ID = model.StoryID;
            dto.FeatureID = model.Feature == null ? "" : model.Feature.FeatureID.ToString();
            dto.Feature = model.Feature == null ? "" : model.Feature.Name;
            dto.FeatureColor = model.Feature == null ? "" : model.Feature.Color;
            dto.Name = model.Name;
            dto.Color = model.Color;
            dto.Size = model.Size;
            dto.Card = model.Card;
            dto.Observations = model.Observations;
            dto.Primary = model.Primary;
            dto.IterationID = model.IterationID;
            dto.CreateTime = model.CreateTime;
            dto.Creator = model.Creator;

            IList tasks = ManagerFactory.TaskManager.GetByStory(model.StoryID.ToString());           
            DefaultStatus? StoryStatus = null;
            Double EvaluationSum = 0.0;
            Double RemainingSum = 0.0;            

            //determine the story status
            foreach (Task task in tasks)
            {
                if (task.Status != null && StoryStatus == null && Enum.IsDefined(typeof(DefaultStatus), task.Status.Name))
                    StoryStatus = (DefaultStatus)Enum.Parse(typeof(DefaultStatus), task.Status.Name);
                else if(StoryStatus == null)
                    StoryStatus = DefaultStatus.InProgress;

                if (task.Status != null && task.Status.Name == DefaultStatus.NotDone.ToString() && StoryStatus == DefaultStatus.NotDone)
                    StoryStatus = DefaultStatus.NotDone;
                else if (task.Status != null && task.Status.Name == DefaultStatus.Done.ToString() && StoryStatus == DefaultStatus.Done)
                    StoryStatus = DefaultStatus.Done;
                else
                    StoryStatus = DefaultStatus.InProgress;

                EvaluationSum += task.Evaluation;
                RemainingSum += task.Remaining;
            }            

            dto.Status = (StoryStatus == null) ? DefaultStatus.NotDone : (DefaultStatus)StoryStatus;
            dto.EvaluationSum = EvaluationSum;
            dto.RemainingSum = RemainingSum;            

            return dto;
        }        

        public static Story MapFromDTO(StoryDTO dto)
        {
            Story Story = new Story();
            Story.StoryID = dto.ID;
            Story.Feature = (dto.FeatureID == null || dto.FeatureID == "") ? null : ManagerFactory.FeatureManager.Get(new Guid(dto.FeatureID));
            Story.Name = dto.Name;
            Story.Color = dto.Color;
            Story.Size = dto.Size;
            Story.CreateTime = DateTime.Now;
            Story.Card = dto.Card;
            Story.Observations = dto.Observations;
            Story.Primary = dto.Primary;
            Story.IterationID = dto.IterationID;
            Story.CreateTime = dto.CreateTime;
            Story.Creator = dto.Creator;

            return Story;
        }
    }
}
 